mini.parse();

function GoodsArticlesHandler() {
};

GoodsArticlesHandler.instance = new GoodsArticlesHandler();

//获得全局的可选文章数据表格对象
var selectGrid = mini.get("selectGrid");

//获得全局的已选文章数据表格对象
var selectedGrid = mini.get("selectedGrid");

// 加载所有文章
selectGrid.load();

// 加载已关联文章
selectedGrid.load({bgiId: bgiId});

//品牌状态数据
GoodsArticlesHandler.brandStateData = [
	{text: '全部', id: -1},
	{text: '无效', id: 0},
	{text: '有效', id: 1}
];

//返回商品列表
GoodsArticlesHandler.returnGoods = function () {
	window.location.href = "goodsList.jsp";
};

/**
 * 刷新页面数据及布局
 */
GoodsArticlesHandler.afresh = function () {
	window.location.href="goodsArticles.jsp?bgiId="+bgiId+"&bgiName="+bgiName;
};

/**
 * 初始化商品查询面板
 */
GoodsArticlesHandler.initQueryForm = function(){
	mini.get("query_baiState").setData(GoodsArticlesHandler.brandStateData);
}

GoodsArticlesHandler.initQueryForm();

/**
 * 执行查询文章的方法
 */
GoodsArticlesHandler.doQueryArticles = function(){
	var queryBagTree = mini.get("queryBagTree").getValue();
	var baiTitle = mini.get("query_baiTitle").getValue();
	var baiState = mini.get("query_baiState").getValue();
    selectGrid.load({baiGroup: queryBagTree, baiTitle: baiTitle, baiState: baiState});
};

function onActionArticles(e) {
	var s = '<a class="New_Button" href="javascript:void(0);" onclick="removeGoodsAts();">删除</a>';
	return s;
}

function add() {        
    var items = selectGrid.getSelecteds();
	doAddItems(items);
}

function addAll() {        
    var items = selectGrid.getData();
    doAddItems(items);
}

function removeGoodsAts() {
    var items = selectedGrid.getSelecteds();
    selectedGrid.removeRows(items);
}   

function doAddItems(items) {
    items = mini.clone(items);
    //根据id判断，去除重复的item
    for (var i = items.length - 1; i >= 0; i-- ) {
        var item = items[i];
        var item2 = selectedGrid.findRow(function (row) {
            if (row.baiId == item.baiId) return true;
        });
        if (item2) {
            items.removeAt(i);
        }
    }
    selectedGrid.addRows(items);
}

function saveItems() {
	var data = selectedGrid.getData();
	data = mini.clone(data);
	var json = mini.encode(data);
	mini.confirm("确认提交更改？" ,"系统提示",
		function(action, value){
			if (action == "ok") {
				var ids = "";
				for ( var i = 0; i < data.length - 1; i++){
					ids += data[i].baiId+ ",";
				}
				if(data.length!=0)
				ids += data[data.length - 1].baiId;
                $.ajax({
                    url: __root + "/addGoodsAricles.do",
                    data : {
		            	baiIds : ids,
		            	bgiId : bgiId,
		            	lslUser: gbOperateUser,
		    			lslAddress: gbOperateIp
		            },
                    success: function (text) {
                    	selectedGrid.unmask();
                    	MiniuiTools.miniInfo("系统提示","操作成功！", function(){
        				selectedGrid.reload();
        			});
                },
                error: function (jqXHR, textStatus, errorThrown) {
            			selectedGrid.unmask();
        				MiniuiTools.miniError("系统提示","操作失败！" + jqXHR.responseText);
                    }
                });	
			} 
		}
	)
}